package com.ruibang.glass.produce.controller.pl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruibang.glass.common.constants.RedisKey;
import com.ruibang.glass.common.domain.camunda.BpmProcessInstanceCreate;
import com.ruibang.glass.frequencycontrol.annotation.RedissonLock;
import com.ruibang.glass.produce.domain.req.RawMaterialReceiveReq;
import com.ruibang.glass.produce.entity.RawMaterialReceive;
import com.ruibang.glass.produce.service.RawMaterialReceiveService;
import com.teaming.cloud.framework2.common.model.ResultBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @Author: songJinKang
 * @CreateTime: 2024-03-11  14:41
 * @Description: TODO
 * @Version: 1.0
 */

@Api(tags = "生产-配料-原材料领料控制器")
@Validated
@RestController
@RequestMapping("/api/glass/produce/pl/rawMaterialReceive")
public class RawMaterialReceiveController {

    @Autowired
    private RawMaterialReceiveService rawMaterialReceiveService;

    @GetMapping("/getById/{id}")
    @ApiOperation(value = "根据ID获取详情", response = ResultBody.class)
    public ResultBody<RawMaterialReceive> getById(@PathVariable String id) {
        return ResultBody.success(rawMaterialReceiveService.getDetails(id));
    }

    @PostMapping("/pageQuery")
    @ApiOperation(value = "查询记录", response = ResultBody.class)
    public ResultBody<IPage<RawMaterialReceive>> pageQuery(@RequestBody @Valid RawMaterialReceiveReq rawMaterialReceiveReq) {
        return ResultBody.success(rawMaterialReceiveService.pageQuery(rawMaterialReceiveReq));
    }

    @PostMapping("/edit")
    @ApiOperation(value = "修改", response = ResultBody.class)
    @RedissonLock(key = RedisKey.RAW_MATERIAL_REDIS_LOCK_KEY, waitTime = 3)
    public ResultBody edit(@RequestBody @Valid RawMaterialReceive rawMaterialReceive) {
        return rawMaterialReceiveService.edit(rawMaterialReceive);
    }

    @PostMapping("/save")
    @ApiOperation(value = "新增", response = ResultBody.class)
    @RdeissonLock(key = RedisKey.RAW_MATERIAL_REDIS_LOCK_KEY, waitTime = 3)
    public ResultBody save(@RequestBody @Valid RawMaterialReceive rawMaterialReceive) {
        return rawMaterialReceiveService.insert(rawMaterialReceive);
    }

    @PostMapping("/startProcess")
    @ApiOperation(value = "发起流程", response = ResultBody.class)
    public ResultBody startProcess(@RequestBody BpmProcessInstanceCreate bpmProcessInstanceCreate) {
        return rawMaterialReceiveService.startProcess(bpmProcessInstanceCreate);
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除", response = ResultBody.class)
    public ResultBody delete(@PathVariable String id) {
        rawMaterialReceiveService.deleteById(id);
        return ResultBody.success();
    }

    @GetMapping("/getOrderNo")
    @ApiOperation(value = "获取申请单号", response = ResultBody.class)
    public ResultBody getOrderNo() {
        return ResultBody.success(rawMaterialReceiveService.getOrderNo());
    }

    @GetMapping("/processNotice/{businessId}")
    @ApiOperation(value = "流程处理通知", response = ResultBody.class)
    public ResultBody processNotice(@PathVariable("businessId") String businessId) {
        rawMaterialReceiveService.processNotice(businessId);
        return ResultBody.success();
    }

    @DeleteMapping("/deleteById/{id}")
    @ApiOperation(value = "删除", response = ResultBody.class)
    public ResultBody deleteById(@PathVariable("id") String id) {
        rawMaterialReceiveService.deleteInfo(id);
        return ResultBody.success();
    }
}
